package com.et114.modules.msg.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.core.action.BaseDispatchAction;
import com.et114.core.utility.DateUtil;
import com.et114.modules.msg.form.MsgForm;
import com.et114.modules.msg.service.IPub_MessageService;
import com.et114.modules.msg.service.Pub_MessageServiceImp;
import com.et114.modules.msg.vo.MsgVO;
import com.et114.modules.org.services.UserManager;
import com.et114.modules.org.utility.SessionUserInfo;
import com.et114.modules.org.vo.UserVo;

/**
 * @author zhiweip
 * @version 1.0
 * @created 16-十二月-2010 13:51:41
 */
public class Pub_messageAction extends BaseDispatchAction {
	private IPub_MessageService	messageServiceImp	= ( Pub_MessageServiceImp ) getBean ( "messageServiceImp" );
	
	@ Override
	public ActionForward unspecified ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		SessionUserInfo sessionuserinfo = getCurrentUser ( request );
		MsgForm mForm=(MsgForm)form;
		MsgVO vo = new MsgVO ( );
		vo.setDeleteflag ( "0" );
		vo.setPageNum ( mForm.getPageNum ( ) );
		vo.setPageSize ( mForm.getPageSize ( ) );
		vo.setRuserid ( sessionuserinfo.getUserId ( ).toString ( ) );
		request.setAttribute ( "DataList" , messageServiceImp
				.queryMsgDataList ( vo ) );
		
		vo.setReadflag ( "0" );
		request.setAttribute ( "NOREAD" , messageServiceImp.countNoread_PUB_MESSAGE ( vo ) );
		return mapping.findForward ( "queryDataList_read" );
	}
	
	public ActionForward read ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		SessionUserInfo sessionuserinfo = getCurrentUser ( request );
		MsgForm mForm=(MsgForm)form;
		MsgVO vo = new MsgVO ( );
		vo.setDeleteflag ( "0" );
		vo.setPageNum ( mForm.getPageNum ( ) );
		vo.setPageSize ( mForm.getPageSize ( ) );
		
		//System.out.println ("=======================isRead=###:"+request.getParameter ( "isRead" ));
		if(request.getParameter ( "isRead" )!=null&&!request.getParameter ( "isRead" ).equals ( "" )){
			vo.setReadflag ( "0" );
			request.setAttribute ( "isRead" , "read" );
		}
		vo.setRuserid ( sessionuserinfo.getUserId ( ).toString ( ) );
		request.setAttribute ( "DataList" , messageServiceImp
				.queryMsgDataList ( vo ) );
		
		MsgVO vo2 = new MsgVO ( );
		vo2.setDeleteflag ( "0" );
		vo2.setReadflag ( "0" );
		vo2.setRuserid ( sessionuserinfo.getUserId ( ).toString ( ) );
		request.setAttribute ( "NOREAD" , messageServiceImp.countNoread_PUB_MESSAGE ( vo2 ) );
		return mapping.findForward ( "queryDataList_read" );
	}
	
	public ActionForward sended ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		SessionUserInfo sessionuserinfo = getCurrentUser ( request );
		MsgForm mForm=(MsgForm)form;
		MsgVO vo = new MsgVO ( );
		vo.setPageNum ( mForm.getPageNum ( ) );
		vo.setPageSize ( mForm.getPageSize ( ) );
		vo.setSenduserid (  sessionuserinfo.getUserId ( ).toString ( ) );
		
		request.setAttribute ( "DataList" , messageServiceImp
				.queryMsgDataList ( vo ) );
		return mapping.findForward ( "queryDataList_send" );
	}
	
	public ActionForward toSend ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		UserManager userManager = ( UserManager )getBean ( "userManager" ) ;
		UserVo user= new UserVo();
		user.setPageSize ( 10000 );
		request.setAttribute ( "EMPLYS" , userManager.getEmpAllList ( user ));
		
		request.setAttribute ( "touser" , request.getParameter ( "to" ) );
		
		return mapping.findForward ( "toSend" );
	}
	
	public ActionForward send ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		SessionUserInfo sessionuserinfo = getCurrentUser ( request );
		MsgVO vo = new MsgVO ( );
		vo.setSenduserid (  sessionuserinfo.getUserId ( ).toString ( ) );
		
		vo.setRuserid ( request.getParameter ( "touser" ) );
		vo.setTitle ( request.getParameter ( "title" ) );
		vo.setContent ( request.getParameter ( "content" ) );
		vo.setSendtime ( DateUtil.getDateOutUSA(null,true) );
		vo.setMtype ( "1" );//普通消息
		messageServiceImp.insertMsgInfo ( vo );
		
		request.setAttribute ( "back" , "Pub_messageAction.go" );
		return mapping.findForward ( "success" );
	}
	
	public ActionForward view ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		MsgVO vo = new MsgVO ( );
		vo.setMid ( new Integer(request.getParameter ( "mid" ).trim ( )) );
		
		request.setAttribute ( "MSG" , messageServiceImp.loadMsg ( vo ) );
		return mapping.findForward ( "view" );
	}
	public ActionForward update ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		SessionUserInfo sessionuserinfo = getCurrentUser ( request );
		MsgVO vo = new MsgVO ( );
		vo.setSenduserid (  sessionuserinfo.getUserId ( ).toString ( ) );
		String mids = request.getParameter ( "mids" );
		String[] m =null; 
		if(mids!=null)
			m = mids.split ( ";" );
		else 
			m=request.getParameterValues ( "mid" );
		
		String op=request.getParameter ( "opType" );
		 
		vo.setOperType ( op );
		messageServiceImp.updateMsgInfo ( vo,m);
		
		vo = new MsgVO ( );
		vo.setDeleteflag ( "0" );
		vo.setRuserid ( sessionuserinfo.getUserId ( ).toString ( ) );
		request.setAttribute ( "DataList" , messageServiceImp
				.queryMsgDataList ( vo ) );
		vo.setReadflag ( "0" );
		request.setAttribute ( "NOREAD" , messageServiceImp.countNoread_PUB_MESSAGE ( vo ) );
		return mapping.findForward ( "queryDataList_read" );
		
	}
}